package com.wangcang.widgetlibrary.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.activity.base.BaseActivity;

public class MyJigsawActivity extends BaseActivity {

    // 两个拆分合并按钮
    Button chaifenBt1 = null;
    Button chaifenBt2 = null;

    // 要拆分和合并的布局
    RelativeLayout com3OneLayout = null; // 第三列第一个布局
    RelativeLayout com3TwoLayout = null; // 第三列第二个布局
    boolean isCom3Hebing = false;  // 是否第三列合并了

    RelativeLayout com2OneLayout = null; // 第二列第一个布局
    RelativeLayout com2TwoLayout = null; // 第二列第二个布局
    RelativeLayout com2ThreeLayout = null; // 第二列第三个布局
    boolean isCom2Chaifen = false; // 是否第二列拆分了

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_jigsaw);

        chaifenBt1 = findView(R.id.chai_hebing_1);
        chaifenBt2 = findView(R.id.chai_hebing_2);
        com3OneLayout = findView(R.id.com3_1);
        com3TwoLayout = findView(R.id.com3_2);
        com2OneLayout = findView(R.id.com2_1);
        com2TwoLayout = findView(R.id.com2_2);
        com2ThreeLayout = findView(R.id.com_2_3);

        OnButtonClick onButtonClick = new OnButtonClick();
        chaifenBt1.setOnClickListener(onButtonClick);
        chaifenBt2.setOnClickListener(onButtonClick);
    }

    class OnButtonClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            if (v == chaifenBt1) {
                isCom3Hebing = !isCom3Hebing;
                if (isCom3Hebing) {
                    com3OneLayout.setVisibility(View.GONE);
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) com3TwoLayout.getLayoutParams();
                    params.weight = 32;
                    com3TwoLayout.setLayoutParams(params);
                }
                else {
                    com3OneLayout.setVisibility(View.VISIBLE);
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) com3TwoLayout.getLayoutParams();
                    params.weight = 16;
                    com3TwoLayout.setLayoutParams(params);
                }
            }
            else if (v == chaifenBt2) {
                isCom2Chaifen = !isCom2Chaifen;
                if (isCom2Chaifen) {
                    com2TwoLayout.setVisibility(View.VISIBLE);
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) com2ThreeLayout.getLayoutParams();
                    params.weight = 31 - 15 + 8; // 把16%的高度给第二列第二个， 同时把第二列第一个压8%
                    com2ThreeLayout.setLayoutParams(params);
                    // 第二列第一个压缩8%
                    LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams) com2OneLayout.getLayoutParams();
                    params1.weight = params1.weight - 8;
                    com2OneLayout.setLayoutParams(params1);
                }
                else {
                    com2TwoLayout.setVisibility(View.GONE);
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) com2ThreeLayout.getLayoutParams();
                    params.weight = 31; // 恢复第二列第三个的百分比
                    com2ThreeLayout.setLayoutParams(params);

                    // 第二列第一个压缩的8%加回来
                    LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams) com2OneLayout.getLayoutParams();
                    params1.weight = params1.weight + 8;
                    com2OneLayout.setLayoutParams(params1);
                }
            }
        }
    }
}
